<?php
/**
 * Created by PhpStorm.
 * User: soft
 * Date: 2016/9/7
 * Time: 17:59
 * extends base_task_abstract implements base_interface_task
 */

//define('ROOT_DIR',realpath(dirname(__FILE__)));
define('ROOT_DIR', realpath(dirname(__FILE__).'/../../../../'));
date_default_timezone_set(
    defined('DEFAULT_TIMEZONE') ? ('Etc/GMT'.(DEFAULT_TIMEZONE>=0?(DEFAULT_TIMEZONE*-1):'+'.(DEFAULT_TIMEZONE*-1))):'UTC'
);

require(ROOT_DIR.'/app/base/kernel.php');
require(ROOT_DIR.'/config/config.php');
kernel::register_autoload();

class b2c_tasks_goodsbagsearch {

    private $type = 4;
    private $cat_type = '98';

    public function set_props_value_gid($m,$r){
        $sql = 'select props_value_id from sdb_b2c_goods_type_props_value order by props_value_id asc';
        $props_value_ids = $m->select($sql);
        foreach($props_value_ids as $_k=>$_v){
            $props_value_id = $_v['props_value_id'];
            $sql = "SELECT g.goods_id FROM `sdb_b2c_goods` g
                 LEFT JOIN  `sdb_b2c_goods_props_extend` p ON (p.goods_id=g.goods_id)
                 WHERE p.`props_value_id` = $props_value_id
                 AND p.goods_p in (SELECT t.goods_p FROM `sdb_b2c_goods_type_props` t WHERE t.props_id IN (SELECT  tp.props_id FROM `sdb_b2c_goods_type_props_value` tp WHERE tp.`props_value_id` = $props_value_id ))
                 AND g.marketable = TRUE and g.goods_type='normal' and g.type_id = $this->type
                 GROUP BY g.`goods_id` HAVING g.goods_id!=''
                 ";

            $arr = $m->select($sql);
            $key = 'props_bag_'.$props_value_id;
            if($r->exists($key)){
                $r->del($key);
            }
            if(!$arr){
                $r->setAdd($key,'');
            }else{
                foreach($arr as $k=>$v){
                    $r->setAdd($key,$v['goods_id']);
                }
            }

            echo '--------props_bag_goods:'.$key.'-------------\n\r';

            unset($props_value_id,$key,$value);
        }

    }

    public function set_brand_gid($m,$r){
        $sql = 'select brand_id from sdb_b2c_brand';
        $brand_ids = $m->select($sql);

        foreach($brand_ids as $_k =>$_v){
            $brand_id = $_v['brand_id'];
            $sql = "SELECT goods_id FROM sdb_b2c_brand b
                LEFT JOIN sdb_b2c_goods g
                ON g.brand_id = b.brand_id
                WHERE b.brand_id = $brand_id and g.marketable = true and g.goods_type='normal' and g.type_id = ".$this->type."
                group by g.goods_id";
            $brand_gids = $m->select($sql);

            $key = 'brand_bag_'.$brand_id;
            if($r->exists($key)){
                $r->del($key);
            }
            if(!$brand_gids){
                $r->setAdd($key,'');
            }else{
                foreach($brand_gids as $k=>$v) {
                    $r->setAdd($key,$v['goods_id']);
                }
            }
            echo '--------brand_bag_goods:'.$key.'-------------\n\r';

            unset($brand_id,$key,$value);
        }
    }
    //获取各个分类下的商品ID
    /**
     * @param $m Model 数据库操作对象
     * @param $r redis
     */
    public function set_cat_gid($m,$r){
        $sql = "select cat_id,cat_name,cat_path from sdb_b2c_goods_cat where type_id = ".$this->type." order by cat_id asc";
        $catInfo = $m->select($sql);

        foreach($catInfo as $_k=>$_v){
            $path = explode(',',$_v['cat_path']);
            if(count($path) == 4){ //只查3级分类下面的商品ID（例如：皮鞋，凉鞋。排除男鞋女鞋童鞋和鞋）
                $cat_id = $_v['cat_id'];
                $sql = "select g.goods_id from sdb_b2c_goods_cat c
                    left join sdb_b2c_goods g
                    on c.cat_id = g.cat_id
                    where c.cat_id = $cat_id and g.marketable = true and g.goods_type='normal' and c.type_id = ".$this->type."
                    group by g.goods_id";
                $cat_gids = $m->select($sql);

                $key = 'cat_bag_'.$cat_id;
                if($r->exists($key)){
                    $r->del($key);
                }
                if(!$cat_gids){
                    $r->setAdd($key,'');
                }else{
                    foreach($cat_gids as $k=>$v){
                        $r->setAdd($key,$v['goods_id']);
                        $r->setAdd('cat_bag_97',$v['goods_id']);
                    }
                }
                echo '--------cat_bag_goods:'.$key.'-------------\n\r';
                unset($cat_gids,$key,$cat_id,$sql);

            }
            if(count($path) == 3){ //只查2级分类下面的商品ID（例如：皮鞋，凉鞋。排除男鞋女鞋童鞋和鞋）
                $cat_id = $_v['cat_id'];
                $sql = "select g.goods_id from sdb_b2c_goods_cat c
                    left join sdb_b2c_goods g
                    on c.cat_id = g.cat_id
                    where c.cat_id
                    in (select cat_id from sdb_b2c_goods_cat where c.parent_id = $cat_id and type_id = \"$this->type\" )
                    and g.marketable = true and g.goods_type='normal' and c.type_id = ".$this->type."
                    group by g.goods_id";

                $cat_gids = $m->select($sql);
                $key = 'cat_bag_'.$cat_id;
                if($r->exists($key)){
                    $r->del($key);
                }
                if(!$cat_gids){
                    $r->setAdd($key,'');
                }else{
                    foreach($cat_gids as $k=>$v){
                        $r->setAdd($key,$v['goods_id']);
                        $r->setAdd('cat_bag_97',$v['goods_id']);
                    }
                }

                echo '--------cat_bag_goods:'.$key.'-------------\n\r';

            }
            unset($cat_id,$key,$sql,$cat_gids,$path);
        }
        unset($catInfo);
    }


    //封装分类详情
    public function set_brands_info($m,$r){
        $sql = 'select brand_id from sdb_b2c_brand';
        $brand_ids = $m->select($sql);
        $brand_ids_str = null;
        foreach ($brand_ids as $key => $value) {
            $brand_ids_str .= $value['brand_id'].',';
        }
        $key = 'brands_bag';
        if($r->exists($key)){
            $r->del($key);
        }
        $value = trim($brand_ids_str,',');
        echo '--------brands_bag:'.$key.'-------------\n\r';
        $r->store($key,$value);
    }

    public function set_cats_info($m,$r){
        $sql = 'select cat_id,cat_name from sdb_b2c_goods_cat where cat_id in('.$this->cat_type.')';
        $p_catinfo = $m->select($sql);
        foreach($p_catinfo as $k=>&$v){
            $sql = "select cat_id,cat_name from sdb_b2c_goods_cat where type_id=".$this->type." and parent_id = ".$v['cat_id'];
            $child_catInfo = $m->select($sql);
            $v['child_catInfo']=$child_catInfo;
        }
        $key = 'cats_bag';
        if($r->exists($key)){
            $r->del($key);
        }
        $value = json_encode($p_catinfo);
        echo '--------cats_bag:'.$key.'-------------\n\r';
        $r->store($key,$value);
    }
    //封装属性值详情
    public function set_props_info($m,$r){
        $sql = 'select props_id,name,type,search,p.show,goods_p from sdb_b2c_goods_type_props p where type_id = '.$this->type;
        $props_info = $m->select($sql);
        foreach($props_info as $k=>&$v){
            $props_id = $v['props_id'];
//            $sql = "SELECT v.props_value_id ,v.`name`
//                 FROM sdb_b2c_goods_type_props_value v
//                 LEFT JOIN `sdb_b2c_goods_type_props` p ON p.`props_id` = v.`props_id`
//                 WHERE p.`props_id` = $props_id and p.type_id=2";
            $sql = "SELECT props_value_id ,`name`
                 FROM sdb_b2c_goods_type_props_value
                 WHERE `props_id` = $props_id";

            $props_value = $m->select($sql);
            $v['props_value_info']=$props_value;
            unset($props_id);
        }
        $value = json_encode($props_info);
        $key = 'props_bag';
        if($r->exists($key)){
            $r->del($key);
        }
        echo '--------props_bag:'.$key.'-------------\n\r';
        $r->store($key,$value);
    }

    public function exec($params=null){
        //在这里写处理代码
        set_time_limit(0);
        $r = kernel::single('base_kvstore_redis2');
        $m =  kernel::database();
        $r->selects(1);
        $this->set_props_info($m,$r);
        $this->set_cats_info($m,$r);
        $this->set_brands_info($m,$r);
        $this->set_brand_gid($m,$r);
        $this->set_cat_gid($m,$r);
        $this->set_props_value_gid($m,$r);
    }
}
$obj = new b2c_tasks_goodsbagsearch();
$obj->exec();